package fdts.calendar.render;

/********************************************************
 * FDTS SS 2012. Calendar Layouter and iCalendar Imorter*
 * ******************************************************/

import java.awt.Font;
import java.awt.Shape;

import prefuse.render.AbstractShapeRenderer;
import prefuse.visual.VisualItem;

import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;

import prefuse.Constants;

/**
 * this class extends AbstractShapeRenderer and implements a shape renderer, 
 * that draws a rectangle in the calendar application in CalendarVisualization.
 * 
 * @author agnieszka wojdecka and andreas hoermann
 *
 */
public class CalendarShapeRenderer extends AbstractShapeRenderer {

    private int m_widthSize = 7;
    private int m_heightSize = 7;
    
    private Rectangle2D m_rect = new Rectangle2D.Double();
    private GeneralPath m_path = new GeneralPath();

    protected Font    m_font; // temp font holder
    
    /**
     * Creates a new ShapeRenderer with default base size of 10 pixels.
     */
    public CalendarShapeRenderer() {
    }
    
    /**
     * Creates a new ShapeRenderer with given base size.
     * @param size the base size in pixels
     */
    public CalendarShapeRenderer(int width,int height) {
       setSize(width,height);
    }
    
    /**
     * Sets the base size, in pixels, for shapes drawn by this renderer. The
     * base size is the width and height value used when a VisualItem's size
     * value is 1. The base size is scaled by the item's size value to arrive
     * at the final scale used for rendering.
     * @param size the base size in pixels
     */
    public void setSize(int width, int height) {
        m_widthSize = width;
        m_heightSize = height;
    }
    
    /**
     * Returns the base size, in pixels, for shapes drawn by this renderer.
     * @return the base size in pixels
     */
    public int[] getBaseSize() {
    	int [] arr = new int [2];
    	arr[0] = m_widthSize;
    	arr[1] = m_heightSize;
        return arr;
    }
    
    /**
     * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)
     */
    protected Shape getRawShape(VisualItem item) {
        int stype = item.getShape();
        double x = item.getX() + (item.getEndX()-item.getStartX())/2;
        if ( Double.isNaN(x) || Double.isInfinite(x) )
            x = 0;
        double y = item.getY() + (item.getEndY()-item.getStartY())/2;
        if ( Double.isNaN(y) || Double.isInfinite(y) )
            y = 0;
        double width = item.getEndX() - item.getStartX();
        double height = item.getEndY() - item.getStartY();
        
        // Center the shape around the specified x and y
        if ( width > 1 && height > 1) {
            x = x-width/2;
            y = y-height/2;
        }
        
        switch ( stype ) {
        case Constants.SHAPE_NONE:
            return null;
        case Constants.SHAPE_RECTANGLE:
            return rectangle(x, y, width, height);
        default:
            throw new IllegalStateException("Unknown shape type: "+stype);
        }
    }

    /**
     * Returns a rectangle of the given dimenisions.
     */
    public Shape rectangle(double x, double y, double width, double height) {
        m_rect.setFrame(x, y, width, height);
        
        return m_rect;
    }

} // end of class ShapeRenderer

